package com.cloudsigns.factorymanager2.engineering.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.activity.PublicationOrderDetailActivity;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringConstructContentBean;
import com.cloudsigns.factorymanager2.engineering.bean.MediaBean;

import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2018/5/5.
 */

public class AddPublicationOrderAdapter extends BaseAdapter {
    private Context context;
    private List<MediaBean> datas = new ArrayList<MediaBean>();
    private LayoutInflater inflater;
    private String tag = "";
    private ItemRemoveListener itemRemoveListener;


    public AddPublicationOrderAdapter(Context context, String tag) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.tag = tag;
    }

    public void setItemRemoveListener(ItemRemoveListener itemRemoveListener) {
        this.itemRemoveListener = itemRemoveListener;
    }


    public void setData(List<MediaBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }

    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public MediaBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.item_add_publication_order_list, null);
            holder.img_remove = convertView.findViewById(R.id.img_remove);
            holder.tv_media_name = convertView.findViewById(R.id.tv_media_name);
            holder.tv_number = convertView.findViewById(R.id.tv_number);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        MediaBean mediaBean = datas.get(position);
        holder.tv_media_name.setText(mediaBean.getMediaName());
        holder.tv_number.setText(mediaBean.getNumber());


        if (tag.equals("PublicationOrderDetailActivity") || tag.equals("PatrolOrderDetailActivity") || tag.equals("ReceivePublicationOrderActivity")
                || tag.equals("ReceivePatrolnOrderActivity") || tag.equals("WorkOrderInfomationForPublicationFragment") || tag.equals("RegionPatrolOrderListActivity")) {
            holder.img_remove.setVisibility(View.GONE);
        }else if(tag.equals("DispatchPatrolOrderDetailActivity")) {
            CommonEnum.AssignStatus assignStatus = mediaBean.getAssignStatus();
            if (null != assignStatus) {
                if (assignStatus == CommonEnum.AssignStatus.CREATED) {
                    holder.img_remove.setVisibility(View.VISIBLE);
                } else if (assignStatus == CommonEnum.AssignStatus.EDITED) {
                    holder.img_remove.setVisibility(View.VISIBLE);
                } else if (assignStatus == CommonEnum.AssignStatus.SEND) {
                    holder.img_remove.setVisibility(View.GONE);

                } else if (assignStatus == CommonEnum.AssignStatus.RECEIVED) {
                    holder.img_remove.setVisibility(View.GONE);
                } else if (assignStatus == CommonEnum.AssignStatus.RETURNED) {
                    holder.img_remove.setVisibility(View.GONE);

                } else if (assignStatus == CommonEnum.AssignStatus.COMPLETED) {
                    holder.img_remove.setVisibility(View.GONE);
                } else {
                    holder.img_remove.setVisibility(View.VISIBLE);

                }
            }

            holder.img_remove.setTag(position);

            holder.img_remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (itemRemoveListener != null) {
                        itemRemoveListener.remove(view);
                    }
                }
            });
        }else {
            holder.img_remove.setVisibility(View.VISIBLE);
            holder.img_remove.setTag(mediaBean);
            holder.img_remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (itemRemoveListener != null) {
                        itemRemoveListener.remove(view);
                    }
                }
            });

        }


        return convertView;
    }

    private class ViewHolder {
        private TextView tv_media_name;
        private ImageView img_remove;
        private TextView tv_number;


    }
}
